package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.Consultation;
import com.ddyx.mapper.ConsultationMapper;
import com.ddyx.service.ConsultationService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Slf4j
@Service
public class ConsultationServiceImpl implements ConsultationService {
	
	@Resource
	ConsultationMapper consultationMapper;

	@Override
	public ResponseForm addConsultation(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			
			Map paramMap = (Map) param.getData();
			Consultation bean = new Consultation();
			MyBeanUtils.populate(bean, paramMap);
			bean.setCreateTime(new Date());
			consultationMapper.insertSelective(bean);

		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Override
	public ResponseForm listConsultation(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				com.ddyx.util.Page.parsePage(paramMap);
				Example example = new Example(Consultation.class);
				example.setOrderByClause("create_time DESC");
				
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				List<Consultation> list=consultationMapper.selectByExample(example);
				result.setData(list);
				result.setTotal((int) pageHelper.getTotal());
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm updateConsultation(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			try {
				
				Map paramMap = (Map) param.getData();
				Consultation bean = new Consultation();
				MyBeanUtils.populate(bean, paramMap);
				bean.setUpdateTime(new Date());
				consultationMapper.updateByPrimaryKeySelective(bean);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}
	
	
}
